package com.example.schoolexam2.controller;

import com.alibaba.fastjson.JSONArray;
import com.example.schoolexam2.dto.LoginDto;
import com.example.schoolexam2.entity.PowerVO;
import com.example.schoolexam2.entity.Student;
import com.example.schoolexam2.enums.ResultEnum;
import com.example.schoolexam2.service.PowerService;
import com.example.schoolexam2.service.UserService;
import com.example.schoolexam2.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userservice;
    @Autowired
    PowerService powerService;

    @PostMapping("/login")
    ResultVO<JSONArray> login(@RequestBody LoginDto dto){
        PowerVO powerVO = new PowerVO();
        powerVO.setUid(dto.getUserID());
        ResultVO<JSONArray> resultVO;
        String token = userservice.userLogin(dto);
        JSONArray JSON = new JSONArray();
        if (token!=null){
            JSONArray power = powerService.getPower(powerVO);
            JSON.add(token);
            JSON.add(power);
            resultVO =new ResultVO<JSONArray>(ResultEnum.LOGIN_SUCCESS.getCode(),ResultEnum.LOGIN_SUCCESS.getMessage(),JSON);
        }else{
            resultVO=new ResultVO<>(ResultEnum.LOGIN_FAILED.getCode(), ResultEnum.LOGIN_FAILED.getMessage(), null);
        }
        return resultVO;
    }
}
